package com.cube.dp.cor.wrapper;

import com.cube.dp.cor.context.TransformContext;
import com.cube.dp.cor.filter.TransformFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

/**
 * @author cube.li
 * @date 2021/12/23 18:01
 * <p>
 * 过滤器执行耗时时间 包装器
 */
@Slf4j
public class TimeConsumingTransformFilterWrapper implements TransformFilterWrapper {

    private final TransformFilter delegate;

    public TimeConsumingTransformFilterWrapper(TransformFilter delegate) {
        Assert.notNull(delegate, "TransformFilter delegate must be not null");
        this.delegate = delegate;
    }

    @Override
    public void doFilter(TransformContext context) {
        final long start = System.currentTimeMillis();
        try {
            delegate.doFilter(context);
        } finally {
            log.debug("过滤器执行耗时{}Ms", System.currentTimeMillis() - start);
        }
    }

    @Override
    public TransformFilter getDelegate() {
        return this.delegate;
    }
}
